package orderclient

import (
	"context"
	"enjoy-shop/formdata"
	"fmt"
	client2 "gitee.com/enjoy_5/goods-rpc/client"
	"gitee.com/enjoy_5/goods-rpc/proto/goods"
	"gitee.com/enjoy_5/order-rpc/client"
	"gitee.com/enjoy_5/order-rpc/proto/order"
	"github.com/google/uuid"
	"strconv"
	"time"
)

// 创建订单
func CreateOrder(in formdata.OrderCreateReq) (*formdata.OrderCreate, error) {
	var orderGoods []*order.OrderGoodsInfo
	var totalPrice float32
	for _, v := range in.GoodsItem {
		ds, err := client2.GetSkuInfoByIDs(context.Background(), v.SkuID)
		if err != nil {
			return &formdata.OrderCreate{}, err
		}
		orderGoods = append(orderGoods, &order.OrderGoodsInfo{
			GoodsId:  v.GoodsID,
			SkuId:    v.SkuID,
			Quantity: v.Num,
			Price:    strconv.FormatFloat(float64(ds.Price), 'f', 2, 32),
		})
		totalPrice += float32(v.Num) * ds.Price
	}
	no := uuid.NewString()
	orders, err := client.CreateOrder(context.Background(), &order.CreateOrderRequest{In: &order.OrderInfo{
		OrderNo:        no,
		Note:           "",
		PayType:        0,
		AddressId:      in.AddressID,
		UserId:         1,
		CouponId:       in.CouponID,
		Amount:         strconv.FormatFloat(float64(totalPrice), 'f', 2, 32),
		OrderGoodsInfo: orderGoods,
	}})
	if err != nil {
		return &formdata.OrderCreate{}, err
	}
	fmt.Printf("Response: %+v\n", orders)
	//通过地址id获取地址信息
	//通过优惠卷id获取优惠金额

	//通过skuId获取商品属性,价格,简介,图片
	var good []formdata.OrderGoodsInfos
	for _, item := range in.GoodsItem {
		ds, err := client2.GetSkuInfoByIDs(context.Background(), item.SkuID)
		if err != nil {
			return &formdata.OrderCreate{}, err
		}
		info, err := client2.GetGoodsInfo(context.Background(), ds.GoodsId)
		if err != nil {
			return &formdata.OrderCreate{}, err
		}
		good = append(good, formdata.OrderGoodsInfos{
			GoodsId:          ds.GoodsId,
			PropertyValueId1: ds.ProValueId1,
			PropertyValueId2: ds.ProValueId2,
			PropertyValueId3: ds.ProValueId3,
			Title:            info.Title,
			Img:              info.Img,
			Price:            strconv.FormatFloat(float64(ds.Price), 'f', 2, 32),
			Num:              item.Num,
		})
	}
	ordergoods := &formdata.OrderCreate{
		CreateTime:     time.Unix(orders.CreateTime, 1),
		OrderNo:        no,
		AddressInfo:    formdata.AddressInfo{},
		OrderGoodsInfo: good,
		CouponInfo:     formdata.CouponInfo{},
	}

	return ordergoods, err
}

// 订单列表
func GetOrderList(userId, status int64) ([]formdata.OrderList, error) {
	list, err := client.GetOrderList(context.Background(), &order.OrderListRequest{
		UserId: userId,
		Status: status,
	})
	if err != nil {
		return nil, err
	}
	var orders []formdata.OrderList
	for _, v := range list {
		var goodsInfo *goods.GoodsInfo
		if len(v.OrderGoodsInfo) > 0 {
			// 获取第一个商品的信息
			info := v.OrderGoodsInfo[0]
			goodsInfo, err = client2.GetGoodsInfo(context.Background(), info.GoodsId)
			if err != nil {
				return nil, err
			}
		}

		// 如果没有商品信息，使用默认值或者根据业务逻辑处理
		img := ""
		title := ""
		if goodsInfo != nil {
			img = goodsInfo.Img
			title = goodsInfo.Title
		}

		orders = append(orders, formdata.OrderList{
			CreateTime: time.Unix(v.CreateTime, 0),
			Status:     v.Status,
			Img:        img,
			Title:      title,
			Amount:     v.Amount,
		})
	}

	return orders, nil
}

// 订单商品列表
func GetOrderGoodsList(orderId int64) (formdata.OrderGoodsList, error) {
	//地址Id获取地址信息
	list, err := client.GetOrderGoodsList(context.Background(), &order.OrderListInfoRequest{OrderId: orderId})
	if err != nil {
		return formdata.OrderGoodsList{}, err
	}
	var good []formdata.OrderGoodsInfos
	for _, item := range list {
		ds, err := client2.GetSkuInfoByIDs(context.Background(), item.SkuId)
		if err != nil {
			return formdata.OrderGoodsList{}, err
		}
		info, err := client2.GetGoodsInfo(context.Background(), item.GoodsId)
		if err != nil {
			return formdata.OrderGoodsList{}, err
		}
		good = append(good, formdata.OrderGoodsInfos{
			GoodsId:          ds.GoodsId,
			PropertyValueId1: ds.ProValueId1,
			PropertyValueId2: ds.ProValueId2,
			PropertyValueId3: ds.ProValueId3,
			Title:            info.Title,
			Img:              info.Img,
			Price:            strconv.FormatFloat(float64(ds.Price), 'f', 2, 64),
			Num:              item.Quantity,
		})
	}
	info, err := client.GetOrderInfo(context.Background(), &order.GetOrderInfoRequest{OrderId: orderId})
	if err != nil {
		return formdata.OrderGoodsList{}, err
	}
	ordergoods := formdata.OrderGoodsList{
		OrderNo:        info.OrderNo,
		CreateTime:     time.Unix(info.CreateTime, 0),
		PayType:        info.PayType,
		AddressInfo:    formdata.AddressInfo{},
		OrderGoodsInfo: good,
	}
	return ordergoods, err
}

// 修改订单状态
func UpdateOrder(id, status int64) error {
	return client.UpdateOrderStatus(context.Background(), &order.OrderUpdateStatusRequest{
		OrderId: id,
		Status:  status,
	})
}

func GetOrder(id int64) (*order.OrderInfo, error) {
	return client.GetOrderInfo(context.Background(), &order.GetOrderInfoRequest{OrderId: id})
}
